package org.tsinghua.answer.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.tsinghua.answer.domain.question.Question;
import org.tsinghua.answer.domain.question.QuestionRepository;
import org.tsinghua.answer.domain.question.QuestionService;
import org.tsinghua.answer.domain.question.Reply;
import org.tsinghua.answer.domain.question.ReplyRepository;
import org.tsinghua.answer.domain.question.exception.ReplyException;
import org.tsinghua.answer.domain.subject.Subject;
import org.tsinghua.answer.domain.subject.SubjectRepository;
import org.tsinghua.answer.domain.user.User;
import org.tsinghua.answer.domain.user.UserType;
import org.tsinghua.answer.utils.AuthUtils;

@Controller
@RequestMapping("question")
public class QuestionController {

	@Autowired
	private SubjectRepository subjectRepository;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private QuestionRepository questionRepository;
	@Autowired
	private ReplyRepository replyRepository;
	
	
	@GetMapping("")
	public ModelAndView question(HttpServletRequest request) {
		ModelAndView view = new ModelAndView("createQuestion");
		List<Subject> subjects = subjectRepository.findAll();
		view.addObject("subjects", subjects);
		return view;
	}
	
	@PostMapping("")
	public ModelAndView question(Question question) {
		String creatorId = AuthUtils.getUser().getId();
		question.setCreatorId(creatorId);
		question = questionService.createQuestion(question);
		String questionId = question.getId();
		return question(questionId);
	}
	
	@GetMapping("{questionId}")
	public ModelAndView question(@PathVariable("questionId") String questionId) {
		User self = AuthUtils.getUser();
		Question question = questionRepository.findOne(questionId);
		List<Reply> replies = replyRepository.findByQuestionId(questionId);
		ModelAndView view = new ModelAndView("question");
		view.addObject("user", self);
		view.addObject("question", question);
		view.addObject("replies", replies);
		if(self.getType() != UserType.USER) {
			String domain = self.getType().toString().toLowerCase();
			view.addObject("domain", domain);
		}
		return view;
	}
	
	@PostMapping("{questionId}")
	public ModelAndView reply(Reply reply) {
		User self = AuthUtils.getUser();
		reply.setCreatorId(self.getId());
		try {
			reply = questionService.reply(reply);
			ModelAndView view = question(reply.getQuestionId());
			view.addObject("success", "保存成功");
			return view;
		} catch (ReplyException e) {
			ModelAndView view = question(reply.getQuestionId());
			view.addObject("error", e.getMessage());
			return view;
		}
	}
	
	@PostMapping("{questionId}/close")
	public String close(@PathVariable("questionId") String questionId) {
		questionService.close(questionId);
		return "redirect:/question/" + questionId;
	}
}
